import logo from "@/assets/images/logo.png";
import { DEFAULT_PRIMARY, SYSTEM_ID } from "@/config/config";
import piniaPersistConfig from "@/config/piniaPersist";
import { filePrefix } from "@/hooks/useSystem";
import { createPinia, defineStore } from "pinia";
import piniaPluginPersistedstate from "pinia-plugin-persistedstate";
import { GlobalState, ThemeConfigProps } from "./interface";
// defineStore 调用后返回一个函数，调用该函数获得 Store 实体
export const GlobalStore = defineStore({
	// id: 必须的，在所有 Store 中唯一
	id: SYSTEM_ID + "_global",
	// state: 返回对象的函数
	state: (): GlobalState => ({
		currentVersion: "",
		setting: {
			//记住用户名
			rememberUser: false,
			//记录的用户名
			username: "",
			isSysTitle: true,
			home: "",
			icon: "default-favicon.ico",
			systemName: "",
			logo: logo
		},
		// element组件大小
		assemblySize: "default", //small/default(medium)/large
		// language
		language: "zh",
		themeConfig: {
			// 布局切换 ==>  纵向：vertical | 经典：classic | 横向：transverse | 分栏：columns | 三列： three
			layout: "three",
			// 默认 primary 主题颜色
			primary: DEFAULT_PRIMARY,
			// 深色模式
			isDark: false,
			// 灰色模式
			isGrey: false,
			// 色弱模式
			isWeak: false,
			// 折叠菜单
			isCollapse: false,
			// 面包屑导航
			breadcrumb: true,
			// 面包屑导航图标
			breadcrumbIcon: false,
			// 标签页
			tabs: false,
			// 标签页图标
			tabsIcon: false,
			// 页脚
			footer: false,
			// 当前页面是否全屏
			maximize: false
		}
	}),
	getters: {},
	actions: {
		setSystemName(systemName: string) {
			this.setting.systemName = systemName || import.meta.env.VITE_GLOB_APP_TITLE;
		},

		setHome(home: string) {
			this.setting.home = home;
		},

		setLogo(logoUrl: string) {
			this.setting.logo = logoUrl || logo;
			this.setting.icon = logoUrl ? filePrefix() + logoUrl : "default-favicon.ico";
		},
		setCurrentVersion(currentVersion: string) {
			this.currentVersion = currentVersion;
		},
		rememberUser(flag: boolean) {
			this.setting.rememberUser = flag;
		},
		// setAssemblySizeSize
		setAssemblySizeSize(assemblySize: string) {
			this.assemblySize = assemblySize ? assemblySize : "default";
		},
		// updateLanguage
		updateLanguage(language: string) {
			this.language = language;
		},
		// setThemeConfig
		setThemeConfig(themeConfig: ThemeConfigProps) {
			this.themeConfig = themeConfig;
		}
	},
	persist: piniaPersistConfig(SYSTEM_ID + "_global")
});

// piniaPersist(持久化)
const pinia = createPinia();
pinia.use(piniaPluginPersistedstate);

export default pinia;
